package com.fashiontextile.common.utils;

import com.fashiontextile.common.config.properties.SnowflakeProperties;
import com.github.yitter.contract.IIdGenerator;
import com.github.yitter.contract.IdGeneratorException;
import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.DefaultIdGenerator;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @author brooke_zb
 */
@Service
@EnableConfigurationProperties(SnowflakeProperties.class)
public class IdUtils {
    private final IIdGenerator idGenInstance;

    public IdUtils(SnowflakeProperties snowflakeProperties) {
        IdGeneratorOptions options = new IdGeneratorOptions(snowflakeProperties.getWorkerId());

        idGenInstance = new DefaultIdGenerator(options);
    }

    public long nextId() throws IdGeneratorException {
        return idGenInstance.newLong();
    }
}
